package com.sun.controller;

import com.alibaba.fastjson.JSON;
import com.sun.entity.Reader;
import com.sun.service.ReaderService;
import com.sun.util.CaptchaUtil;
import jakarta.servlet.http.HttpServlet;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.OutputStream;

/**
 * @author SunRui
 * @description TODO
 * @date 2024/8/10 22:49
 */
@Controller
@RequestMapping("/ReaderController")
public class ReaderController{
    @Autowired
    private ReaderService readerService;
    private static Logger logger = Logger.getLogger(BookController.class);
    private String jsonString;
    private String str;

    @RequestMapping("/sendCaptcha")
    public void sendCaptcha(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession();
        CaptchaUtil captchaUtil = new CaptchaUtil();
        String captcha = captchaUtil.generateCaptcha();
        session.setAttribute("captcha", captcha);
        logger.debug("验证码:" + captcha);
        jsonString = JSON.toJSONString("ok");
        resp.getWriter().write(jsonString);
    }

    @RequestMapping("/login")
    public void login(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession();
        String email = req.getParameter("email");
        String pwd = req.getParameter("pwd");
        String captcha2 = req.getParameter("captcha");
        logger.debug("登录邮箱:" + email + ";登录密码:" + pwd + ";登录验证码:" + captcha2);
        //提取验证码
        String captcha3 = (String) session.getAttribute("captcha");
        CaptchaUtil captchaUtil = new CaptchaUtil();
        if (captchaUtil.captchaEquals(captcha3, captcha2)) {
            session.removeAttribute("captcha");
            Reader reader = readerService.login(email, pwd);
            if (reader != null) {
                session.setAttribute("reader", reader);
                logger.debug("登录的账号信息:" + reader.toString());
                str = "ok";
            } else {
                str = "loginNo";
            }
        } else {
            str = "captchaNo";
        }
        jsonString = JSON.toJSONString(str);
        resp.getWriter().write(jsonString);
    }

    @RequestMapping("/register")
    public void register(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession();
        String email2 = req.getParameter("email");
        String pwd2 = req.getParameter("pwd");
        String captcha4 = req.getParameter("captcha");
        String name = req.getParameter("name");
        logger.debug("注册名称:"+name+";注册邮箱:" + email2 + ";注册密码:" + pwd2 + ";注册验证码:" + captcha4);
        //提取验证码
        String captcha5 = (String) session.getAttribute("captcha");
        CaptchaUtil captchaUtil = new CaptchaUtil();
        if (captchaUtil.captchaEquals(captcha5, captcha4)) {
            session.removeAttribute("captcha");
            Reader reader = new Reader();
            reader.setName(name);
            reader.setEmail(email2);
            reader.setPwd(pwd2);
            reader.setMoney(0);
            reader.setBorrowSum(5);
            if (readerService.addReader(reader)>0){
                str = "ok";
            }else {
                str = "no";
            }
        } else {
            str = "captchaNo";
        }
        jsonString = JSON.toJSONString(str);
        resp.getWriter().write(jsonString);
    }

    @RequestMapping("/myLoad")
    public void myLoad(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession();
        //获取登录的账号
        Reader reader = (Reader) session.getAttribute("reader");
        String emails = reader.getEmail();
        String pwds = reader.getPwd();
        Reader readers = readerService.login(emails, pwds);
        logger.debug("提取的账号信息:"+readers);
        jsonString = JSON.toJSONString(readers);
        resp.getWriter().write(jsonString);
    }

    @RequestMapping("/topUpMoney")
    public void topUpMoney(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession();
        //充值
        int money = Integer.parseInt(req.getParameter("money"));
        logger.debug("前端传回充值数据:"+money);
        Reader reader1 = (Reader) session.getAttribute("reader");
        if (readerService.topUpMoney(reader1.getEmail(),money)>0){
            str = "ok";
        }
        //将后台的用户信息刷新
        Reader reader3 = (Reader) session.getAttribute("reader");
        Reader reader2 = readerService.login(reader3.getEmail(),reader3.getPwd());
        session.setAttribute("reader",reader2);
        jsonString = JSON.toJSONString(str);
        resp.getWriter().write(jsonString);
    }

    @RequestMapping("/changePwd")
    public void changePwd(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession();
        //密码修改
        String changePwd = req.getParameter("newPwd2");
        logger.debug("前端传回的新密码:"+changePwd);
        Reader reader4 = (Reader) session.getAttribute("reader");
        String oldPwd = req.getParameter("pwd");
        if (!oldPwd.equals(reader4.getPwd())){
            str = "oldno";
        }else if (readerService.updatePwd(reader4.getId(),changePwd)>0){
            str = "ok";
        }
        jsonString = JSON.toJSONString(str);
        resp.getWriter().write(jsonString);
    }
}